﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DormMS.IService;
using DormMS.Model;
using DormMS.Model.SearchModels;
using DormMS.Model.ViewModels;
using Infrastructure.Dao;
using SqlSugar;

namespace DormMS.Service
{
    public class RoomService : IRoomService
    {
        private SqlSugarClient db;
        
        /// <summary>
        /// 通过Id找公寓信息
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public Room GetRoomInfoById(int Id)
        {
            using(db = DbConfig.GetInstance())
            {                
                var data = db.Queryable<Room>().SingleOrDefault(m => m.Id == Id);
                return data;
            }
        }
        /// <summary>
        /// 获取公寓性质
        /// </summary>
        /// <returns></returns>
        public List<SysCode> GetRoomAttr()
        {
            using (db = DbConfig.GetInstance())
            {

                var data = db.Queryable<SysCode>().Where(x => x.CodeType == "公寓性质").ToList();
                return data;
            }
        }
        
        public List<V_Room> GetRoomList(RoomSearchModel search, ref int total)
        {
            using (db = DbConfig.GetInstance())
            {
                db.CurrentFilterKey = "";
                search.OrderByFields = search.OrderByFields;

                var data = db.Queryable<Room>().JoinTable<Building>((m, s2) => m.BuildingId == s2.Id).JoinTable<SysCode>((m, s3) => m.RoomAttr == s3.Code).Where<Room, SysCode>((m, s3) => s3.CodeType == "公寓性质").JoinTable<SysCode>((m, s4) => m.RoomType == s4.Code).Where<Room, SysCode>((m, s4) => s4.CodeType == "公寓类型").Select<Room, Building, SysCode, SysCode, V_Room>((m, s2, s3, s4) => new V_Room { Id = m.Id, RoomNO = m.RoomNO, BuildingId = m.BuildingId, RoomTypeName = s3.CodeName, RoomAttrName = s4.CodeName, Status = m.Status, BuildingNO = s2.BuildingNO });

                if (string.IsNullOrEmpty(search.Keywords) == false)
                {
                    data = data.Where<V_Room>(x => x.BuildingNO == search.Keywords || x.RoomNO == search.Keywords);
                }

                var list = data.OrderBy(search.OrderByFields).ToPageList(search.Skip / search.Take + 1, search.Take, ref total);
                return list;
            }
                
        }

        /// <summary>
        /// 获取公寓类型
        /// </summary>
        /// <returns></returns>
        public List<SysCode> GetRoomType()
        {
            using (db = DbConfig.GetInstance())
            {
                var data = db.Queryable<SysCode>().Where(x => x.CodeType == "公寓类型").ToList();
                return data;
            }
        }
        /// <summary>
        /// 通过NO和大楼Id找公寓信息
        /// </summary>
        /// <param name="RoomNO"></param>
        /// <param name="BuildingID"></param>
        /// <returns></returns>
        public Room GetRoomInfoByNOAndBuildingID(string NO, string BuildingID)
        {
            using (db = DbConfig.GetInstance())
            {
                int intBuildingID = Convert.ToInt32(BuildingID);
                var data = db.Queryable<Room>().SingleOrDefault(m => m.RoomNO == NO && m.BuildingId == intBuildingID);
                return data;
            }
        }

        /// <summary>
        /// 新增公寓信息
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int RoomAdd(Room model)
        {
            using (db = DbConfig.GetInstance())
            {
                int result = db.Insert<Room>(model).ObjToInt();
                AddBed(model);
                return result;

            }
        }
        /// <summary>
        /// 删除公寓信息
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int RoomDel(Room model)
        {
            using (db = DbConfig.GetInstance())
            {
                return db.Update<Room>(model).ObjToInt();
            }
        }
        /// <summary>
        /// 编辑公寓信息
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int RoomEdit(Room model)
        {
            using (db = DbConfig.GetInstance())
            {

                int result = db.Update<Room>(model).ObjToInt();
                AddBed(model);
                return result;
            }
        }

        public int RoomAddImport(List<Room> listRoomImport)
        {
            using (db = DbConfig.GetInstance())
            {

                return db.InsertRange<Room>(listRoomImport).ObjToInt();
            }
        }
        /// <summary>
        /// 获取公寓所有信息
        /// </summary>
        /// <returns></returns>
        public List<Room> GetRoomAllList()
        {
            using (db = DbConfig.GetInstance())
            {
                var data = db.Queryable<Room>().OrderBy("BuildingID,Id").ToList();
                return data;
            }
        }

        /// <summary>
        /// 增加床位，算法，为了简单，只用增加处理，如果公寓床位变少则无边化，取值时则取对应床位数，多处来床位不取，床位增加是则增加床位。
        /// </summary>
        /// <param name="model"></param>
        private void AddBed(Room model)
        {
            int bedNum = 0;
            if (model.RoomType == 1)
            {
                bedNum = 2;
            }
            else if (model.RoomType == 2)
            {
                bedNum = 4;
            }
            else if (model.RoomType == 3)
            {
                bedNum = 6;
            }
            else if (model.RoomType == 4)
            {
                bedNum = 8;
            }

            List<Bed> bedList = new List<Bed>();
            db.CurrentFilterKey = "";

            //判断公寓里面有没有床位
            var data = db.Queryable<Bed>().Where(x => x.BuildingId == model.BuildingId && x.RoomId == model.Id).ToList();

            if (data == null)
            {
                //没有床位
                bedList.Clear();
                for (int i = 1; i < bedNum; i++)
                {
                    Bed bed = new Bed();
                    bed.RoomId = model.Id;
                    bed.BuildingId = model.BuildingId;
                    bed.BedNO = i.ToString().PadLeft(2, char.Parse("0"));
                    bed.CupboardNO = i.ToString().PadLeft(2, char.Parse("0"));
                    bedList.Add(bed);
                }
            }
            else
            {
                //有床位
                if (data.Count == bedNum)
                {
                    //床位相等
                }
                else if (data.Count < bedNum)
                {
                    //目前床位小于设置床位
                    bedList.Clear();
                    db.CurrentFilterKey = "";                    
                    for (int i = data.Count + 1; i <= bedNum; i++)
                    {
                        Bed bed = new Bed();
                        bed.RoomId = model.Id;
                        bed.BuildingId = model.BuildingId;
                        bed.BedNO = i.ToString().PadLeft(2, char.Parse("0"));
                        bed.CupboardNO = i.ToString().PadLeft(2, char.Parse("0"));
                        bedList.Add(bed);
                    }
                }
                else if (data.Count > bedNum)
                {
                    //目前床位大于设置床位
                }
            }
            db.InsertRange<Bed>(bedList).ObjToInt();
        }
    }
}
